import { Component, OnInit } from '@angular/core';
import { TreeTableModule } from 'primeng/treetable';
import { TreeNode } from 'primeng/api';
import { MixedService } from 'src/app/service/mixed.service';
import { select, Store } from '@ngrx/store';
import { cloneDeep } from 'lodash';
import { getCity } from 'src/app/store/utils/utils.reducer';

@Component({
    selector: 'app-mixed-report',
    templateUrl: './mixed-report.component.html',
    styleUrls: ['./mixed-report.component.scss'],
})
export class MixedReportComponent implements OnInit {
    data: TreeNode[];
    searchTimeType = '月';
    searchTime = new Date();
    genTwoLengthNumberString = (n) => (n >= 10 ? n : '0' + n);
    dateTimeTrans(d) {
        if (!d) {
            return '';
        }
        let yy = d.getFullYear();
        let MM = this.genTwoLengthNumberString(d.getMonth() + 1);
        let dd = this.genTwoLengthNumberString(d.getDate());
        if (this.searchTimeType === '日') {
            return yy + '-' + MM + '-' + dd;
        }
        if (this.searchTimeType === '月') {
            return yy + '-' + MM;
        }
        if (this.searchTimeType === '年') {
            return yy;
        }
    }
    timeTypeTrans(str) {
        if (str === '日') {
            return 'day';
        } else if (str === '月') {
            return 'month';
        } else if (str === '年') {
            return 'year';
        }
        return 'unrecgonized';
    }
    async search() {
        let city;
        this.store.pipe(select(getCity)).subscribe((data) => {
            city = data;
        });
        console.log(city);
        const res = await this.mixedService.fetchCityMixed({
            month: this.dateTimeTrans(this.searchTime),
            list: city.map((item) => {
                return item.zoneId;
            }),
        });
        if (res) {
            const n = Date.now();
            const r = res.map((item, idx) => {
                return {
                    ...item,
                    ...city[idx],
                    name: city[idx].city,
                    hasChildren: true,
                    id: 'city' + city[idx].zoneId + n,
                };
            });
            console.log(r);
            // data.value = []
            this.data = r;
        }
    }

    constructor(private mixedService: MixedService, private store: Store) {}

    ngOnInit(): void {
        this.search();
    }
}
